package com.aierjun.line;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.widget.EditText;

import com.aierjun.notepadjun.R;


public class LineDeitText extends EditText {

    protected Paint linePaint;
    private int lineColor=Color.GRAY;
    private int lineHeight=1;
    private int lineHeightOffset=0;

    public LineDeitText(Context context) {
        this(context,null);
    }

    public LineDeitText(Context context, AttributeSet attrs) {
        this(context, attrs,0);
    }

    public LineDeitText(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        lineHeightOffset = (int)(0.19753087f * getLineHeight() + 0.5f);

        TypedArray ta=context.obtainStyledAttributes(attrs, R.styleable.LineDeitText);
        lineColor=ta.getColor(R.styleable.LineDeitText_lineColor,lineColor);
        lineHeight=ta.getDimensionPixelSize(R.styleable.LineDeitText_lineHeight,lineHeight);
        lineHeightOffset=ta.getDimensionPixelSize(R.styleable.LineDeitText_lineHeightOffset,lineHeightOffset);
        ta.recycle();

        linePaint=new Paint();
        linePaint.setStrokeWidth(lineHeight);
        linePaint.setColor(lineColor);
    }

    protected void onDraw(Canvas canvas){
        super.onDraw(canvas);
        int lineCount=getLineCount();
        int height =getHeight();
        int lineHeight=getLineHeight();

        lineCount=Math.max(lineCount,1+height/lineHeight);
        int lineY=getCompoundPaddingTop()+lineHeightOffset;
       // int m=height/lineHeight;
        //if (lineCount<m) lineCount=m;
        //int lineY=getCompoundPaddingTop()+lineHeight;

        for (int i=0;i<=lineCount;i++){
            lineY+=lineHeight;
            canvas.drawLine(getCompoundPaddingLeft(),lineY,getRight()-getCompoundPaddingRight(),lineY,this.linePaint);
            canvas.save();
        }
        super.onDraw(canvas);
    }
}
